package com.linzm;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Date: 2024/1/11 22:01
 * @User linziming
 */
public class OkhttpDemo {
    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();

        String url = "https://restapi.amap.com/v3/weather/weatherInfo?city=110000&key=f97b54c83df8808dfd53d2810a166f5e";

        Request request = new Request.Builder()
                .url(url)
                .build();

        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                String responseBody = response.body().string();
                System.out.println(responseBody);
            } else {
                System.out.println("请求失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test() {
        User user = new User();
        user.setId("l0039280");
        user.setName("linziming");
        String jsonString = JSON.toJSONString(user);
        jsonString = "l0039280," + "linziming," + "fdfkdfjdf";
        // 加密字符串
        String encryptedToken = Base64.encodeBase64String(jsonString.getBytes());

        // 输出加密后的字符串
        System.out.println("加密后的字符串：" + encryptedToken);

        // 解密字符串
        String decryptedToken = new String(Base64.decodeBase64(encryptedToken));

        String newSt = decryptedToken + " re";
        // 输出解密后的字符串
        System.out.println("解密后的字符串：" + newSt);
        User user1 = null;
        try {
            user1 = JSON.parseObject(newSt, User.class);
        } catch (Exception e) {
            throw new RuntimeException("sk不合法");
        }
        System.out.println(user1);
    }

    @Test
    public void test2() {
        // AES 加密
        String aesKey = "1234567890123456"; // AES密钥，长度必须为16位
        String aesData = "Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!"; // AES加密数据
        String aesEncryptedData = SecureUtil.aes(aesKey.getBytes()).encryptHex(aesData); // AES加密后的数据
        System.out.println("AES加密后的数据：" + aesEncryptedData);

        // AES 解密
        String aesDecryptedData = SecureUtil.aes(aesKey.getBytes()).decryptStr(aesEncryptedData); // AES解密后的数据
        System.out.println("AES解密后的数据：" + aesDecryptedData);

        // DES 加密
        String desKey = "12345678"; // DES密钥，长度必须为8位
        String desData = "Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!"; // DES加密数据
        String desEncryptedData = SecureUtil.des(desKey.getBytes()).encryptHex(desData); // DES加密后的数据
        System.out.println("DES加密后的数据：" + desEncryptedData);

        // DES 解密
        String desDecryptedData = SecureUtil.des(desKey.getBytes()).decryptStr(desEncryptedData); // DES解密后的数据
        System.out.println("DES解密后的数据：" + desDecryptedData);
    }

    @Test
    public void test3() {
        String input = "ec79c2a429d261a8e5085773dce1d853667d49bdb22926384cf7718b94c70c20b6f1df1079a82de7d9a19552d03a21229006d69019e4b8ee145ef3b74bc9969a815540de72405f1ea502055d4a4c15456ec48a9d6ee99eb212d990be087c32740e083e283fa57c9942e2fcb75d7d9bff";
        String compressed = compress(input);
        System.out.println("压缩后的字符串：" + compressed);
        String decompressed = decompress(compressed);
        System.out.println("解压缩后的字符串：" + decompressed);
    }

    private static String compress(String input) {
        Map<Character, Integer> charCountMap = new HashMap<>();
        StringBuilder compressedString = new StringBuilder();
        for (char c : input.toCharArray()) {
            charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
        }
        for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
            compressedString.append(entry.getKey()).append(entry.getValue());
        }
        return compressedString.toString();
    }

    private static String decompress(String compressed) {
        StringBuilder decompressedString = new StringBuilder();
        int count = 0;
        for (int i = 0; i < compressed.length(); i++) {
            char c = compressed.charAt(i);
            if (Character.isDigit(c)) {
                count = count * 10 + (c - '0');
            } else {
                for (int j = 0; j < count; j++) {
                    decompressedString.append(c);
                }
                count = 0;
            }
        }
        return decompressedString.toString();
    }
}
